rm(list = ls())

library(SetMethods)

setwd("~/Google Drive/My Drive/University/Research/Writing/03. Projects underway/06. Niche theory paper/Rewriting after ECPR")

DSniche <- read.csv("Nichesall.csv", header = T, row.names = 1)
head(DSniche)
DSniche[10] <- NULL #get rid of the weird extra column

DSnicheyoung <- read.csv("Nichesyoung.csv", header = T, row.names = 1)
head(DSnicheyoung)
DSnicheyoung[10] <- NULL #get rid of the weird extra column

DSnicheold <- read.csv("Nichesold.csv", header = T, row.names = 1)
head(DSnicheold)

##### DESCRIPTIVE STATISTICS #####
hist(DSniche$MEM, main = "Membership niches in all groups", xlab = "Use of membership niche")
hist(DSnicheyoung$MEM, main = "Membership niches in young CSOs", xlab = "Use of membership niche")
hist(DSnicheold$MEM, main = "Membership niches in old CSOs", xlab = "Use of membership niche")

hist(DSniche$FIN, main = "Funding niches in all CSOs", xlab = "Use of funding niche")
hist(DSnicheyoung$FIN, main = "Funding niches in young CSOs", xlab = "Use of funding niche")
hist(DSnicheold$FIN, main = "Funding niches in old CSOs", xlab = "Use of funding niche")
# FIN = subsidies or grants covering the past 3 years

hist(DSniche$FIN2, main = "Funding niches in all CSOs", xlab = "Use of funding niche")
hist(DSnicheyoung$FIN2, main = "Funding niches in young CSOs", xlab = "Use of funding niche")
hist(DSnicheold$FIN2, main = "Funding niches in old CSOs", xlab = "Use of funding niche")
# FIN2 = subsidies or ongoing grants make up >33% of income

hist(DSniche$STR, main = "Strategic niches in all CSOs", xlab = "Use of strategic niche")
hist(DSnicheyoung$STR, main = "Strategic niches in young CSOs", xlab = "Use of strategic niche")
hist(DSnicheold$STR, main = "Strategic niches in old CSOs", xlab = "Use of strategic niche")

hist(DSniche$ISS, main = "Issue niches in all CSOs", xlab = "Use of issue niche")
hist(DSnicheyoung$ISS, main = "Issue niches in young CSOs", xlab = "Use of issue niche")
hist(DSnicheold$ISS, main = "Issue niches in old CSOs", xlab = "Use of issue niche")

##### ALL ORGANISATIONS TOGETHER #####
### Typology building 

# With first FIN measure
TTnoben1 <- truthTable(DSniche, outcome = "OUT",
                      conditions = c("MEM", "FIN", "STR", "ISS"),
                      incl.cut = 0.75, 
                      show.cases = T, 
                      sort.by = c("out", "incl", "n"), 
                      complete = T)
TTnoben1

# Minimise
solnoben1 <- minimize(TTnoben1, use.tilde = T, 
                      details = T)
solnoben1

## Cluster analysis ##

clusternoben1 <- cluster(data = DSniche, solnoben1, "OUT", 
                        unit_id = "ORGANISATION",
                        cluster_id = "TYPE")
clusternoben1

# With second FIN measure (robustness check)
# Create truth table
TTnoben2 <- truthTable(DSniche, outcome = "OUT",
                    conditions = c("MEM", "FIN2", "STR", "ISS"),
                    incl.cut = 0.75, 
                    show.cases = T, 
                    sort.by = c("out", "incl", "n"), 
                    complete = T)
TTnoben2

# Minimise
solnoben2 <- minimize(TTnoben2, use.tilde = T, 
                 details = T)
solnoben2

## Cluster analysis ##
clusternoben2 <- cluster(data = DSniche, solnoben2, "OUT", 
                    unit_id = "ORGANISATION",
                    cluster_id = "TYPE")
clusternoben2

##### FOR YOUNG ORGANISATIONS ONLY ####
# For first FIN measure
# Create truth table
TTyoung1 <- truthTable(DSnicheyoung, outcome = "OUT",
                  conditions = c("MEM", "FIN", "STR", "ISS"), 
                  incl.cut = 0.75, 
                  show.cases = T, 
                  sort.by = c("out", "incl", "n"),
                  complete = T)
TTyoung1

# Minimise
solyoung1 <- minimize(TTyoung1, use.tilde = T, 
                 details = T)
solyoung1

# For second FIN measure (robustness check)
# Create truth table
TTyoung2 <- truthTable(DSnicheyoung, outcome = "OUT",
                      conditions = c("MEM", "FIN2", "STR", "ISS"), 
                      incl.cut = 0.75, 
                      show.cases = T, 
                      sort.by = c("out", "incl", "n"),
                      complete = T)
TTyoung2

# Minimise
solyoung2 <- minimize(TTyoung2, use.tilde = T, 
                     details = T)
solyoung2

##### FOR OLD ORGANISATIONS ONLY ####
# For first FIN measure
# Create truth table
TTold1 <- truthTable(DSnicheold, outcome = "OUT",
                      conditions = c("MEM", "FIN", "STR", "ISS"),
                      incl.cut = 0.75, 
                      show.cases = T, 
                      sort.by = c("out", "incl", "n"),
                      complete = T)
TTold1

# Minimise
solold <- minimize(TTold1, use.tilde = T, 
                     details = T)
solold

# For second FIN measure (robustness check)
# Create truth table
TTold2 <- truthTable(DSnicheold, outcome = "OUT",
                    conditions = c("MEM", "FIN2", "STR", "ISS"),
                    incl.cut = 0.75, 
                    show.cases = T, 
                    sort.by = c("out", "incl", "n"),
                    complete = T)
TTold2

# Minimise
solold2 <- minimize(TTold2, use.tilde = T, 
                   details = T)
solold2
